package com.splatter;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;

import com.chat.chatter.ejb.Emf;
import com.chat.chatter.ejb.Splat;
import com.chat.chatter.ejb.User;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;


public class UserServiceImpl implements UserService {

	public String getUser( String user ) {
		//my try
		EntityManager em = Emf.get().createEntityManager();
		
		List existing = new ArrayList();
		String id = "";
		String key = "";
	    String name = "";
	    String data = "";
		StringBuffer sb = new StringBuffer("");
		try {
			sb.append("<br><br>\n");
			sb.append("<table>\n");
			sb.append("<th>name</th><th>key</th><th>data</th>\n");
			
			
			
	        // ... do stuff with em ...
			if ( null == em ) {
                System.out.println( "entityManagerFactory is hosed" );
			}
			
			existing = em.createQuery(
		    		"select u from User u where u.username = :username")
		    		.setParameter("username", user)
		    		.getResultList();

		    if ( existing.size() == 0 ) {
		    	System.out.println("!!!NO USER EXIST!!!");
		    }
		    else {
		    	System.out.println(existing);
		    	System.out.println("!!!USER EXIST!!!");		    	
		    	
		    	if ( existing.size() == 1 ) {
		    		User dbUser = ( User )existing.get( 0 );
		    		Set< Splat > splats = dbUser.getSplats();
		    		
		    		for ( Splat existingSplat : splats ) {
		    			sb.append("<tr>\n");
		    			if ( existingSplat != null ) {
		    				if ( existingSplat.getUser() != null ) {
		    					name = existingSplat.getUser().getName();
		    					id = Long.toString( existingSplat.getUser().getUserId() );
		    				}
		    				
		    				sb.append("<td>" + name + "</td>");
		    				
		    				if ( existingSplat.getSplat() != null ) {
		    					Key splatKey = null;								
								if ( existingSplat.getSplatId() != null ) {
									splatKey = existingSplat.getSplatId();
									if ( splatKey != null )
										key = Long.toString( splatKey.getId() );
									
									sb.append("<td><a href=\"http://localhost:8080/playClip.jsp?userId=" + id + "&key=" + key + "\">play</a></td>\n");
								}
		    					if ( existingSplat.getSplat().toString()/*.getValue()*/ != null )
		    						data = existingSplat.getSplat().toString()/*.getValue()*/;
		    					
		    					sb.append("<td>" + data + "</td>\n");
		    				}
		    			}
		    			sb.append("</tr>\n");
		    		}
		    	}
		    }
	    } finally {
	        em.close();
	    }
	    
	    
	    sb.append("</table>\n");
	    
		/*return "<br><br>" +
			   "<table>" +			   		
			        "<th>name</th><th>key</th><th>data</th>" +
		            "<tr>" +
		                 "<td>" + name + "</td>" +
		                 "<td>" + key + "</td>" +
		                 "<td><a href=\"http://locahost:8080/play.jsp?play=" + data + "\">play</a></td>" +
		            "</tr>" +
			   "</table>";*/
	    return sb.toString();
	}
}
